package com.zbkj.admin.service.impl;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.zbkj.admin.service.AdminClientUserService;
import com.zbkj.common.constants.Constants;
import com.zbkj.common.exception.CommonException;
import com.zbkj.common.model.user.FeedbackInfo;
import com.zbkj.common.model.user.Partner;
import com.zbkj.common.model.user.User;
import com.zbkj.common.model.user.UserInvoice;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.user.AdminClientUserRequest;
import com.zbkj.common.request.user.AdminUserPwdRequest;
import com.zbkj.common.request.user.AdminUserQueryRequest;
import com.zbkj.common.response.user.AdminClientUserResponse;
import com.zbkj.common.response.user.FeedbackInfoResonse;
import com.zbkj.common.utils.CommonUtil;
import com.zbkj.common.utils.DateUtil;
import com.zbkj.service.service.user.FeedbackInfoService;
import com.zbkj.service.service.user.PartnerService;
import com.zbkj.service.service.user.UserInvoiceService;
import com.zbkj.service.service.user.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Auther:
 * @Date: 2024/7/11 15:58
 * @Description:
 */
@Service
public class AdminClientUserServiceImpl implements AdminClientUserService {

    @Autowired
    private UserService userService;
    @Autowired
    private PartnerService partnerService;
    @Autowired
    private UserInvoiceService userInvoiceService;
    @Autowired
    private FeedbackInfoService feedbackInfoService;

    @Override
    public PageInfo<AdminClientUserResponse> list(AdminUserQueryRequest queryRequest) {
       PageInfo<User> userPage = userService.adminGetList(queryRequest);
       List<AdminClientUserResponse> responseList = Lists.newArrayList();
       for(User user : userPage.getList()) {
           AdminClientUserResponse response = new AdminClientUserResponse();
           BeanUtils.copyProperties(user,response);
           responseList.add(response);
       }
       return CommonPage.copyPageInfo(userPage, responseList);
    }

    @Override
    public void userSave(AdminClientUserRequest userRequest) {
        userRequest.setId(null);
        if(userService.accountRepeatCheck(userRequest.getId(), userRequest.getPhone())) {
            throw new CommonException("登录账户名重复");
        }
        /*if(userRequest.getValidType().intValue() == 0 && Objects.isNull(userRequest.getValidToDate())) {
            throw new CommonException("请填写效结束日期");
        }*/
        User user = new User();
        BeanUtils.copyProperties(userRequest, user);
        if(StringUtils.isNotBlank(userRequest.getInviteCode())) {
            Partner partner = partnerService.getByIviteCode(userRequest.getInviteCode());
            if(Objects.isNull(partner)) {
                throw new CommonException("邀请码不存在");
            }
            user.setInviterId(partner.getId());
        }
        user.setAccount(userRequest.getPhone());
        user.setPwd(CommonUtil.encryptPassword(user.getPwd(),CommonUtil.SERCRET_KEY));
        /*if(Objects.nonNull(userRequest.getValidToDate())) {
            user.setValidToDate(DateUtil.strToDate(userRequest.getValidToDate(), Constants.DATE_FORMAT));
        }*/
        userService.userSaveOrUpdate(user);
    }

    @Override
    public void userUpdate(AdminClientUserRequest userRequest) {
        userRequest.setPwd(null);
        User userDb = userService.getUserById(userRequest.getId());
        if(Objects.isNull(userDb) || userDb.getDeleteFlag() != 0) {
            throw new CommonException("用户信息已不存在");
        }
        if(!StringUtils.equals(userRequest.getPhone(),userDb.getAccount())) {
            if(userService.accountRepeatCheck(userRequest.getId(), userRequest.getPhone())) {
                throw new CommonException("登录账户名重复");
            }
        }
        /*if(userRequest.getValidType().intValue() == 0 && Objects.isNull(userRequest.getValidToDate())) {
            throw new CommonException("请填写效结束日期");
        }*/
        User updateUser = new User();
        BeanUtils.copyProperties(userRequest, updateUser);
        updateUser.setAccount(userRequest.getPhone());
        updateUser.setInviteCode(null);
        /*if(Objects.nonNull(userRequest.getValidToDate())) {
            updateUser.setValidToDate(DateUtil.strToDate(userRequest.getValidToDate(), Constants.DATE_FORMAT));
        }*/
        userService.userSaveOrUpdate(updateUser);
    }

    @Override
    public AdminClientUserResponse getUser(Long userId) {
        User userDb = userService.getUserById(userId);
        if(Objects.isNull(userDb) || userDb.getDeleteFlag() != 0) {
            throw new CommonException("用户信息已不存在");
        }
        AdminClientUserResponse response = new AdminClientUserResponse();
        BeanUtils.copyProperties(userDb,response);
        return response;
    }

    @Override
    public void delUser(Long userId) {
        User userDb = userService.getUserById(userId);
        if(Objects.isNull(userDb) || userDb.getDeleteFlag().intValue() != 0) {
            return;
        }
        User user = new User();
        user.setId(userId);
        user.setDeleteFlag(1);
        user.setAccount(String.format("%s_d%s",userDb.getAccount(),userId));
        userService.updateUserInfo(user);
    }

    @Override
    public void statusUpdate(Long userId,Integer status) {
        if(status !=0 && status != 1) {
            throw new CommonException("状态参数错误");
        }
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        userService.updateUserInfo(user);
    }

    @Override
    public void changePwd(AdminUserPwdRequest pwdRequest) {
        User user = new User();
        user.setId(pwdRequest.getUserId());
        user.setPwd(CommonUtil.encryptPassword(pwdRequest.getPwd(),CommonUtil.SERCRET_KEY));
        userService.updateUserInfo(user);
    }

    @Override
    public UserInvoice getUserInvoice(Long userId) {
        UserInvoice userInvoice = userInvoiceService.getInvoiceByUserId(userId);
        return userInvoice;
    }

    @Override
    public PageInfo<FeedbackInfoResonse> adminGetFeedbackList(PageParamRequest queryRequest) {
        return feedbackInfoService.adminGetFeedbackList(queryRequest);
    }
}
